package com.china.produce.system.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class QueryDslBuildUtil {

    private QueryDslBuildUtil(){

    }

    public static JSONObject buildTimeStampQueryJson(String minute){
        return ESQueryJsonBuilderUtil.buildRangeQuery("@timestamp","now-" + minute + "m/s" ,"now/s" ,true,true,"strict_date_optional_time");
    }

    public static JSONObject buildTimeStampQueryJson(Object startTime, Object endTime){
        return ESQueryJsonBuilderUtil.buildRangeQuery("@timestamp",startTime ,endTime,true,true,null);
    }


    public static JSONObject buildMustQueryString(JSONObject ...jo ){
        JSONArray ja = new JSONArray();
        for (JSONObject j : jo){
            ja.add(j);
        }
        return ESQueryJsonBuilderUtil.buildBoolQuery( ja ,null,null);
    }

    public static JSONObject buildMustQueryString(JSONArray ja, JSONObject... jo){
        for (JSONObject j : jo){
            ja.add(j);
        }
        return ESQueryJsonBuilderUtil.buildBoolQuery( ja ,null,null);
    }

    public static JSONObject buildMustQueryString(JSONArray ja, JSONArray jn, JSONObject... jo){
        for (JSONObject j : jo){
            ja.add(j);
        }
        return ESQueryJsonBuilderUtil.buildBoolQuery( ja ,jn,null);
    }

    public static JSONArray buildQueryString(Map<String,Object> map){
        JSONArray ja = new JSONArray();
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            String key = it.next();
            Object value = map.get(key);
            ja.add(ESQueryJsonBuilderUtil.buildQueryString(key,value.toString()));
        }
        return ja;
    }

    public static JSONArray buildMatch(Map<String,Object> map){
        JSONArray ja = new JSONArray();
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            String key = it.next();
            Object value = map.get(key);
            ja.add(ESQueryJsonBuilderUtil.buildMatch(key,value.toString()));
        }
        return ja;
    }

    public static JSONArray buildNotMatch(Map<String,Object> map){
        JSONArray ja = new JSONArray();
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            String key = it.next();
            Object value = map.get(key);
            Arrays.stream(value.toString().split("&&")).forEach(e -> ja.add(ESQueryJsonBuilderUtil.buildNotMatch(key,e)));
        }
        return ja;
    }

    public static JSONObject buildLastDSL(JSONObject mustQueryString){
        Map<String,Object> map = new HashMap<>();
        map.put("track_total_hits",true);
        return ESQueryJsonBuilderUtil.buildQuery(new ArrayList<>(),mustQueryString,0,0,map);
    }

    public static JSONObject buildLastDSL(JSONObject mustQueryString, Map<String, Object> aggMap){
        return ESQueryJsonBuilderUtil.buildQuery(new ArrayList<>(),mustQueryString,0,0,aggMap);
    }
}
